﻿@model PermissionMappingModel
@{
    //page title
    ViewBag.Title = T("Admin.Configuration.ACL").Text;
}
<form asp-area="@Constants.AreaAdmin" asp-controller="Security" asp-action="Permissions" method="post">
    
    <div class="row">
        <div class="col-md-12">
            <div class="x_panel light form-fit">
                <div class="x_title">
                    <div class="caption">
                        <i class="fa fa-user-secret"></i>
                        @T("Admin.Configuration.ACL")
                    </div>
                    <div class="actions">
                        <button class="btn btn-success" type="submit" name="save"><i class="fa fa-check"></i> @T("Admin.Common.Save") </button>
                        <button class="btn btn-success" type="submit" name="save-continue"><i class="fa fa-check-circle"></i> @T("Admin.Configuration.ACL.InstallMissingPermisions") </button>
                    </div>
                    <vc:admin-widget widget-zone="permission_list_buttons" additional-data="null" />
                </div>
                <div class="x_content form">
                    <div class="form-horizontal">
                        <div class="form-body">
                            <div class="form-group">
                                <div class="col-md-12 col-sm-12">
                                    @if (Model.AvailablePermissions.Count == 0)
                                    {
                                        <text>No permissions defined</text>
                                    }
                                    else if (Model.AvailableCustomerRoles.Count == 0)
                                    {
                                        <text>No customer roles available</text>
                                    }
                                    else
                                    {
                                        <script>
                                            $(document).ready(function () {
                                                @foreach (var cr in Model.AvailableCustomerRoles)
                                                {
                                                    <text>
                                                    $('#selectall-@(cr.Id)').click(function () {
                                                        $('.allow_@(cr.Id)').prop('checked', $(this).is(':checked')).change();
                                                    });
                                                    </text>
                                                }
                                            });
                                        </script>
                                        <table class="table table-bordered table-striped table-hover tableFixHead">
                                            <thead>
                                                <tr>
                                                    <th scope="col">
                                                        <strong>@T("Admin.Configuration.ACL.Permission")</strong>
                                                    </th>
                                                    @foreach (var cr in Model.AvailableCustomerRoles)
                                                    {
                                                        <th scope="col">
                                                            <strong>@cr.Name</strong>
                                                            <label class='mt-checkbox mt-checkbox-outline control control-checkbox'><input type="checkbox" id="selectall-@(cr.Id)" /> <div class="control__indicator"></div></label>
                                                        </th>
                                                    }
                                                </tr>
                                            </thead>
                                            <tbody>
                                                
                                                @{
                                                    bool altRow = true;
                                                }
                                                @foreach (var pr in Model.AvailablePermissions)
                                                {
                                                    altRow = !altRow;
                                                    <tr class="@(altRow ? "altrowstyle" : "rowstyle")">
                                                        <td>
                                                            <span>@pr.Name</span>
                                                        </td>
                                                        @foreach (var cr in Model.AvailableCustomerRoles)
                                                        {
                                                            var allowed = Model.Allowed.ContainsKey(pr.SystemName) && Model.Allowed[pr.SystemName][cr.Id];
                                                            <td style="text-align:center">
                                                                <label class='mt-checkbox mt-checkbox-outline control control-checkbox'><input class="allow_@(cr.Id)" type="checkbox" value="@(pr.SystemName)" name="allow_@(cr.Id)" @(allowed ? " checked=checked" : null) /> <div class="control__indicator"></div></label>
                                                                @if (pr.Actions)
                                                                {
                                                                    <a class='k-link' 
                                                                       onclick="javascript:OpenWindow('@Url.Action("PermissionsAction", "Security")/?systemname=@(pr.SystemName)&customeRoleId=@cr.Id', 600, 400, true); return false;"><i class="icon-settings"></i></a>
                                                                }
                                                            </td>
                                                        }
                                                    </tr>
                                                }
                                            </tbody>
                                        </table>
                                    }
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
</form>